package app;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * Class for parsing txt file to words and there occurencie's frequence. Input file should be in work folder
 * (same where class is or CLASSPATH). Outputs .csv.
 */

public class WordsFrequencyCalculator {
    ArrayList <String> all_parsed_words = new ArrayList <String>();
    private HashSet<WordStatisticHolder> unique_parsed_words = new HashSet<WordStatisticHolder>();
    String designator;
    private File input_file;
    private StringBuilder all_parsed_symbols = new StringBuilder();

    public WordsFrequencyCalculator(String input_file, String d){
        this.input_file=new File (input_file);
        this.designator = d;
        if (this.input_file.isFile()==false) {
            System.out.println("Input file is incorrect!! Wait for Exception:)");
        }
    }

    public WordsFrequencyCalculator(String input_file){
        this.input_file=new File (input_file);
        this.designator = ";";
        if (this.input_file.isFile()==false) {
            System.out.println("Input file is incorrect!! Wait for Exception:)");
        }
    }

    private void parseFile () {
        try {
            FileReader fileReader = new FileReader(input_file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line = null;
            StringBuilder word_element = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                line.trim().replaceAll(" +", " ").toLowerCase(); //trims first space at line and replace multiple spaces with one - than transform all to lower case
                all_parsed_symbols.delete(0, all_parsed_symbols.length()).append(line);
                for (int i = 0; i < all_parsed_symbols.length(); i++) {
                    char symbol = all_parsed_symbols.charAt(i);
                    if (Character.isLetter(symbol)) {
                        word_element.append(symbol);
                    }
                    else {
                        if (word_element.length()>0) {
                            all_parsed_words.add(word_element.toString());
                            word_element.delete(0, word_element.length());
                        }
                    }
                }
            }
            bufferedReader.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        for (int i = 0; i < all_parsed_words.size(); i++) {
            unique_parsed_words.add(new WordStatisticHolder(all_parsed_words.get(i), all_parsed_words, designator));
        }
    }

    private void writeToFile (){
        String output_name = input_file.getName().replaceFirst("[.][^.]+$", "")+"_processed.csv";
        try {
            System.out.print("Output file created at " + input_file.getParent());
            File output_file = new File(input_file.getParent(), output_name);
            output_file.createNewFile();
            FileWriter fileWriter = new FileWriter(output_file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            for (WordStatisticHolder item:unique_parsed_words){
                bufferedWriter.write(item.toString() + "\n");
            }
            bufferedWriter.close();
        }
        catch (IOException E) {
            E.printStackTrace();
        }
    }

    public static void main (String[] args) {
        WordsFrequencyCalculator lol = new WordsFrequencyCalculator("Unix commands.txt");
        lol.parseFile();
        lol.writeToFile();
    }
}